const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
// const HtmlWebpackScriptCrossoriginPlugin = require('html-webpack-script-crossorigin-plugin');

const clientConfig = {
    devtool: process.env.NODE_ENV === 'development' ? '' : 'hidden-source-map',
    entry: {
        main: './js/pages/main.js',
        vendor: ['vue', 'vue-router', 'axios', 'vuex', 'element-ui', 'dayjs']
    },
    resolve: {
        extensions: ['.js', '.vue', '.json'],
        alias: {
            vue$: 'vue/dist/vue.esm.js',
        }
    },
    output: {
        path: process.env.NODE_ENV === 'development' ? path.resolve(__dirname, '../CrawlManagerWebServer/public/dev') : path.resolve(__dirname, '../CrawlManagerWebServer/public/dist'),
        publicPath: process.env.NODE_ENV === 'development' ? '/dev/' : '//cdn.upchinaproduct.com/info/CrawlManagerWebServer/dist/',
        filename: process.env.NODE_ENV === 'development' ? '[name].js' : '[name].[chunkhash:8].js',
        chunkFilename: process.env.NODE_ENV === 'development' ? '[name].js' : '[name].[chunkhash:8].js',
    },
    module: {
        rules: [{
            test: /\.vue$/,
            // exclude: /node_modules/,
            loader: 'vue-loader'
        }, {
            test: /\.js$/,
            exclude: /(node_modules)/,
            loader: 'babel-loader?cacheDirectory'
        }, {
            test: /\.ejs$/,
            exclude: /node_modules/,
            loader: 'art-template-loader'
        }, {
            test: /\.(css|scss)$/,
            // exclude: /node_modules/,
            use: process.env.NODE_ENV === 'development' ? ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use: 'css-loader!postcss-loader'
            }) : ExtractTextPlugin.extract({
                fallback: 'style-loader',
                loader: 'css-loader?localIdentName=[local]!postcss-loader'
            })
        }, {
            test: /\.(png|jpg|jpeg|gif)$/,
            use: [{
                loader: 'url-loader',
                options: {
                    limit: 1,
                    name: process.env.NODE_ENV === 'development' ? '[name].[ext]' : '[name]-[hash:8].[ext]'
                }
            }]
        }, {
            test: /\.(woff|woff2|eot|ttf|otf)$/,
            use: [{
                loader: 'file-loader'
            }]
        }]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: `"${process.env.NODE_ENV}"`
            }
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: ['vendor', 'manifest'],
            minChunks: Infinity,
        }),
        new HtmlWebpackPlugin({
            title: '爬虫配置平台(新)',
            inject: 'body',
            extra: {
                content: '<%include indexContent.ejs%>',
                baidu: '<%include common/baidu.ejs%>',
                description: '优品财富',
            },
            filename: '../../views/index.ejs',
            chunks: ['manifest', 'vendor', 'main'],
            data: {
                userInfo: '<%-JSON.stringify(userInfo)%>',
            },
            template: path.resolve(__dirname, './index.tpl')
        }),
        new ExtractTextPlugin({
            filename: process.env.NODE_ENV === 'development' ? '[name].css' : '[name].[contenthash:8].css',
            allChunks: true
        }),
    ],
    externals: {
        laydate: 'window.laydate'
    },
};

// 发布特殊逻辑
if (process.env.NODE_ENV !== 'development') {
    clientConfig.plugins.push(new webpack.optimize.UglifyJsPlugin({
        sourceMap: true,
        compress: {
            warnings: false
        }
    }));
    clientConfig.plugins.push(new CleanWebpackPlugin(
        ['dist'], // 匹配删除的文件
        {
            root: path.resolve(__dirname, '../CrawlManagerWebServer/public/'), // 根目录
            verbose: true, // 开启在控制台输出信息
            dry: false // 启用删除文件
        }
    ));
    // 在HTML的script标签中加入crossorigin="anonymous"
    // clientConfig.plugins.push(
    //     new HtmlWebpackScriptCrossoriginPlugin({
    //         srcOrigin: 'cdn.upchinaproduct.com' //可以是数组，也可以是string
    //     })
    // );
}

module.exports = clientConfig;
